<template>
	<view>
		<mc-framework navTitle="设备管理" @changeLevel="changeLevel" class="framebzwork">

			<template #content>
				<view class="content_pagingBox">


					<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" class="z_paging"
						:style="{ height: scrollLiftHeight + 'px'}">
						<template #top>
							<view class="z-paging_top">
								<view class="z-paging_top_child" @click="cellClick('返回')">
									<u-icon name="arrow-left"></u-icon>
									<text>返回</text>
								</view>
								<u-search placeholder="搜索" v-model="searchKeyword" @search="handleSearch"
									@custom='handleSearch'></u-search>
							</view>
						</template>
						<view class="" v-for="(item,index) in dataList" :key="index">
							<u-cell :title="item.name" @click="cellClick(item)"></u-cell>
						</view>
					</z-paging>
				</view>
			</template>
		</mc-framework>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchKeyword: '',
				obj: {},
				dataList: [],
				filteredList: [],
				scrollLiftHeight: uni.getSystemInfoSync(), //列表高度
			};
		},
		onLoad(onlod) {
			let that = this;
			var obj = JSON.parse(onlod.obj)
			this.obj = obj
			this.$nextTick(() => {
				uni.createSelectorQuery().select('.content_pagingBox').boundingClientRect(res => {
					that.scrollLiftHeight = res.height;
					console.log(that.scrollLiftHeight);

				}).exec()
			});

		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {
			handleSearch() {
				let that = this;
				if (!that.searchKeyword.trim()) {
					that.dataList = [...that.filteredList]
					return
				}

				const keyword = that.searchKeyword.toLowerCase()
				console.log(keyword);
				that.dataList = that.filteredList.filter(item => {
					return item.name.toLowerCase().includes(keyword)
				})
				that.$refs.paging.complete(that.dataList);
			},
			cellClick(item) {
				if (item != '返回') {
					item.type = this.obj.type
				}
				let pages = getCurrentPages()
				let prevPage = pages[pages.length - 2]
				prevPage.$vm.getValue(item)
				uni.navigateBack({
					delta: 1
				})


			},
			queryList(pageNo) {
				let that = this;
				this.obj.param.PageIndex = pageNo
				console.log(this.obj);
				that.Request.request(this.obj.cmd, this.obj.param, function(ret) {
					console.log(ret);
					if (ret.status == 200) {
						var item
						if (that.obj.type == '产品') {
							item = {
								ProductID: 0,
								ProductCode: "全部",
								ProductName: "全部",
								name: '全部',
								ProductSpec: null
							}
							ret.data.ds.unshift(item)

							ret.data.ds.map(item => item.name = item.ProductName)
						} else if (that.obj.type == '机台') {
							item = {
								EquipmentID: 0,
								EquipmentName: "全部",
								EquipmentCode: "全部",
								name: '全部',
								FWorkshopID: null
							}
							ret.data.ds.unshift(item)
							ret.data.ds.map(item => item.name = item.EquipmentName)
						} else if (that.obj.type == '人员') {
							ret.data.ds.map(item => item.name = item.UserName)
						} else if (that.obj.type == '模具') {
							ret.data.ds.map(item => item.name = item.MoludCode)
						} else if (that.obj.type == '维修人员') {
							ret.data.ds.map(item => item.name = item.EmplName)
						}
						that.$refs.paging.complete(ret.data.ds);
						that.filteredList = [...ret.data.ds]
					}
				})
			},
			changeLevel(data, row) {
				let mesRouteInfo = this.$store.state.GuyueModule.mesRouteInfo;
				let routes = getCurrentPages(); //当前页面栈
				let backIndex = routes.length - mesRouteInfo.routeIndex; // 计算返回页面栈数
				uni.navigateBack(backIndex)
			},
		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";

	.content_pagingBox {
		width: 100%;
		height: 100%;
	}

	.z-paging_top {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 1%;
		box-sizing: border-box;

		.z-paging_top_child {
			display: flex;
			align-items: center;
			margin-right: 2%;
		}
	}
</style>